package action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.opensymphony.xwork2.ActionContext;

import service.TodoService;
import entity.Todo;

public class SelectAllAction {
	private Integer id;
	private String name;
	private List<Todo> list = new ArrayList<Todo>();
	private int pageNo =1;
	private int pageSize = 5;
	private String leixing;
	private Map<String,Object> map=new HashMap<String,Object>();
	private Todo todo;
	private String error;
	public String getError() {
		return error;
	}
	public void setError(String error) {
		this.error = error;
	}
	public Todo getTodo() {
		return todo;
	}
	public void setTodo(Todo todo) {
		this.todo = todo;
	}
	public Map<String, Object> getMap() {
		return map;
	}
	public void setMap(Map<String, Object> map) {
		this.map = map;
	}
	public String getLeixing() {
		return leixing;
	}
	public void setLeixing(String leixing) {
		this.leixing = leixing;
	}
	public List<Todo> getList() {
		return list;
	}
	public void setList(List<Todo> list) {
		this.list = list;
	}
	public int getPageNo() {
		return pageNo;
	}
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public TodoService getTodoService() {
		return todoService;
	}
	public void setTodoService(TodoService todoService) {
		this.todoService = todoService;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	private TodoService todoService;
	
	public String selectAll(){
		list = todoService.selectAllTodosByPb(pageNo, pageSize);
		map=ActionContext.getContext().getSession();
		
		int count = todoService.selectCount();
		int page=0;
		if(count%pageSize==0){
			page=count/pageSize;
		}else{
			page=count/pageSize+1;
		}
		map.put("count", count);
		map.put("page", page);
		map.put("pageNo", pageNo);
		map.put("pageSize", pageSize);
		
		System.out.println(map);
		System.out.println(list);
		
		return "selectAllJsp";
	}
	
	public String selectAllById() throws Exception{
			
			
		todo=todoService.selectTodoById(todo.getId());
		GsonBuilder builder = new GsonBuilder();
		Gson gson = builder.create();
		String strJson = gson.toJson(todo);
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out;
		try {
			response.setContentType("application/json");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			out.write(strJson);
			out.flush();
			System.out.println(strJson+"xixi");
			System.out.println(todo+"hehe");
			System.out.println(todo.getId()+"haha");
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
}
	
	public String selectTodoByName() throws IOException{
		
	  	list= todoService.selectTodoByName(name);
	  	
		Object str = list.get(0);

			
	    
	    	HttpServletResponse response = ServletActionContext.getResponse();
	    	response.setCharacterEncoding("utf-8");
	    	PrintWriter out= response.getWriter();
	    	out.println(str);

		return null;
  }
}